﻿using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using ExcelRefresh;
using ExcelRefresh.Excel;

namespace ExcelRefreshService
{
    public partial class ExcelRefresher : ServiceBase
    {
        private FileScavenger _queueEngine;
        private ExcelRefreshEngine _refresherEngine;
        private readonly ConcurrentQueue<FileToRefresh> _queue;

        public ExcelRefresher()
        {
            InitializeComponent();
            _queue = new ConcurrentQueue<FileToRefresh>();
        }

        protected override void OnStart(string[] args)
        {
            _queueEngine = new FileScavenger(Settings.FoldersToScan, _queue);
            
            _refresherEngine = new ExcelRefreshEngine(
                Settings.MaxThreads, 
                Settings.RefreshTimeout, 
                _queue);
        }

        protected override void OnStop()
        {
            _refresherEngine.Dispose();
            _refresherEngine = null;
            _queueEngine.Dispose();
            _queueEngine = null;
        }
    }
}